package com.shuai.frame.factory;

import com.shuai.frame.config.XSRpcApplicationContext;
import com.shuai.frame.proxy.MockServiceProxy;
import com.shuai.frame.proxy.ServiceProxy;

import java.lang.reflect.Proxy;

public class ServiceProxyFactory {
    public static <T> T getProxy(Class<T> clazz) {
        if (XSRpcApplicationContext.getXSRpConfig().getMock()) {
            return getMockProxy(clazz);
        }
        return (T) Proxy.newProxyInstance(
                clazz.getClassLoader(),
                new Class[]{clazz},
                new ServiceProxy()
        );
    }

    private static <T> T getMockProxy(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(
                clazz.getClassLoader(),
                new Class[]{clazz},
                new MockServiceProxy()
        );
    }
}
